package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.Brand;
import org.smr.ministore.entities.vo.Assign2OrganVO;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.BrandOrganService;
import org.smr.ministore.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;


@Controller("BrandController")
public class BrandController {

	@Resource(name="brandServiceImpl")
	private BrandService service;

	@Resource(name="brandOrganServiceImpl")
	private BrandOrganService brandOrganService;


	/**
	 * 保存编辑后的Brand信息
	 * @param Brand
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.PATCH, value = "/Brand/edit")
	public Message modify(@RequestBody Brand Brand){
		Message msg = new Message();
		
		if(null == Brand.getId()){
			msg.setStatus(service.save(Brand));
		}else{
			msg.setStatus(service.updateById(Brand));
		}
		return msg;
	}
	
	/**
	* 跳转到列表查询Brand
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/Brand/list-page")
	public String toList(){
		return "/Brand/Brand-list";
	}

	/**
	*  根据id查询Brand
	* @return
	*/
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/Brand")
	public Brand get( Long id ){
		return service.queryById(id);
	}


    /**
	 * 分页查询Brand信息
	 * @param Brand
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Brand/list")
	public Pager<Brand> list(@RequestBody Brand Brand , long pageSize, long pageNum ){

		return service.paginate(Brand, pageSize, pageNum);

	}
	
	
	/**
	 * 根据ID删除Brand信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Brand/remove",method = RequestMethod.DELETE)
	public Message remove( Long id){
		
		Message msg = new Message();
		msg.setStatus(service.deleteById(id));
		return msg;
	}

	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/Brand/assign")
	public Message assignTo(@RequestBody Assign2OrganVO assign2Organ){
		Message msg = new Message();
		msg.setStatus(this.service.assign(assign2Organ));
		return msg;
	}

	@ResponseBody
	@RequestMapping( method = RequestMethod.PUT, value = "/Brand/assign2Organs")
	public Message assign2Organs(@RequestBody Assign2OrgansVO assign2Organs){
		Message msg = new Message();
		msg.setStatus(this.service.assignToOrgans(assign2Organs));
        msg.setSuccessful(true);
		return msg;
	}


}
